/*
Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.
 */

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        vector<string> results;
        unordered_map <string, list<string>> smap;
        for (auto it = strs.begin(); it < strs.end(); it++) {
            string key = *it;
            // sort the string
            sort(key.begin(), key.end());
            smap[key].push_back(*it);
        }
        for (auto it = smap.begin(); it != smap.end(); it++) {
            list<string> &l = it->second;
            // found a group of words
            if (l.size() > 1) {
                for (auto lit = l.begin(); lit != l.end(); lit++) {
                    results.push_back(*lit);
                }
            }
        }
        return results;
    }
};
